package com.jss.lanouhn.fristapp.activity;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RadioButton;

import com.jss.lanouhn.fristapp.R;
import com.jss.lanouhn.fristapp.fragments.SearchLeftFragment;
import com.jss.lanouhn.fristapp.fragments.SearchRightFragment;
import com.jss.lanouhn.fristapp.modles.EventBusMsg;

import org.greenrobot.eventbus.EventBus;

public class SearchThingsActivity extends AppCompatActivity implements View.OnClickListener {
    private FragmentManager manager;
    private FragmentTransaction transaction;
    private RadioButton radioButton_left, radioButton_right;
    private ImageView iv_back, iv_search;
    private EditText editText;
    private PopupWindow popupWindow;
    private String[] data;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_things);
        data = new String[]{"默认排序", "按热度排序", "价格由低到高", "价格由高到低"};
        initView();
        initEvent();
        initViewPop();
    }

    private void initView() {
        radioButton_left = (RadioButton) findViewById(R.id.rb_search_left);
        radioButton_right = (RadioButton) findViewById(R.id.rb_search_right);
        iv_back = (ImageView) findViewById(R.id.btn_back_credit1);
        iv_search = (ImageView) findViewById(R.id.iv_select_things);
        editText = (EditText) findViewById(R.id.et_search_things);
        Bundle bundle = this.getIntent().getExtras();
        String text = bundle.getString("name");
        editText.setText(text);

        manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();
        transaction.replace(R.id.ll_search_things, new SearchLeftFragment());
        transaction.commit();
        radioButton_left.setOnClickListener(this);
        radioButton_right.setOnClickListener(this);
        iv_back.setOnClickListener(this);
        iv_search.setOnClickListener(this);

    }

    private void initEvent() {
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length()==0) {
                    SearchThingsActivity.this.finish();
                }
            }
            @Override
            public void afterTextChanged(Editable s) {
            }
        });

    }

    @Override
    public void onClick(View v) {
        manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();
        switch (v.getId()) {
            case R.id.rb_search_left:
                data = new String[]{"默认排序", "按热度排序", "价格由低到高", "价格由高到低"};
                transaction.replace(R.id.ll_search_things, new SearchLeftFragment());
                initViewPop();
                break;
            case R.id.rb_search_right:
                data = new String[]{"默认排序", "按热度排序"};
                transaction.replace(R.id.ll_search_things, new SearchRightFragment());
                initViewPop();
                break;
            case R.id.iv_select_things:
                popupWindow.showAsDropDown(iv_search);
                break;
            case R.id.btn_back_credit1:
                finish();
                break;
        }
        transaction.commit();
    }

    private void initViewPop() {
        View view = LayoutInflater.from(this).inflate(R.layout.search_popwindow, null);
        ListView listView = (ListView) view.findViewById(R.id.lv_search_pop);
        listView.setAdapter(new ArrayAdapter<String>(this, R.layout.item_only_text_pop, R.id.tv_pop_search, data));
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String[] message = new String[]{"", "hot", "price:asc", "price:desc"};
                String msg = message[position];
                EventBus.getDefault().post(new EventBusMsg(msg));
                popupWindow.dismiss();
            }
        });
        //通过view 和宽·高，构造PopopWindow
        popupWindow = new PopupWindow(view, 260,
                ViewGroup.LayoutParams.WRAP_CONTENT, false);
        //此处为popwindow 设置背景，同事做到点击外部区域，popwindow消失
        popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
        // 设置允许在外点击消失
        popupWindow.setOutsideTouchable(true);
        //刷新状态
        popupWindow.update();
        //设置焦点为可点击
        popupWindow.setFocusable(true);
    }
}
